Explicitly close files.
authoremellor@ewan <emellor@ewan>
Tue, 4 Oct 2005 22:56:42 +0000 (23:56 +0100)
committeremellor@ewan <emellor@ewan>
Tue, 4 Oct 2005 22:56:42 +0000 (23:56 +0100)
Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/xend/server/SrvDaemon.py

index 4060b1b88fb4586c35a55926cf062a5cd0c2d2f9..71ae1c3aa36b7bacd9f6281960e14d6a2b398b2f 100644 (file)
@@ -71,14 +71,17 @@ class Daemon:
         @param pidfile: file to read
         @return pid or 0
         """
-        pid = 0
         if os.path.isfile(pidfile) and os.path.getsize(pidfile):
             try:
-                pid = open(pidfile, 'r').read()
-                pid = int(pid)
+                f = open(pidfile, 'r')
+                try:
+                    return int(f.read())
+                finally:
+                    f.close()
             except:
-                pid = 0
-        return pid
+                return 0
+        else:
+            return 0
 
     def find_process(self, pid, name):
         """Search for a process.
@@ -146,8 +149,10 @@ class Daemon:
         if self.child:
             # Parent
             pidfile = open(pidfile, 'w')
-            pidfile.write(str(self.child))
-            pidfile.close()
+            try:
+                pidfile.write(str(self.child))
+            finally:
+                pidfile.close()
 
         return self.child
 
@@ -200,8 +205,10 @@ class Daemon:
         if self.fork_pid(XEND_PID_FILE):
             os.close(w)
             r = os.fdopen(r, 'r')
-            s = r.read()
-            r.close()
+            try:
+                s = r.read()
+            finally:
+                r.close()
             if not len(s):
                 ret = 1
             else: